package com.itheima.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.TokenHolder;
import com.itheima.dao.DishFlavorMapper;
import com.itheima.dao.DishMapper;
import com.itheima.dao.SetMealDishesMapper;
import com.itheima.dao.SetMealMapper;
import com.itheima.pojo.DishFlavor;
import com.itheima.pojo.SetMeal;
import com.itheima.pojo.SetMealDishes;
import com.itheima.service.SetMealService;
import com.itheima.vo.DishDetailVo;
import com.itheima.vo.SetMealPageVo;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Administrator
 */
@Service
@RestControllerAdvice
public class SetMealServiceImpl implements SetMealService {


    @Resource
    private SetMealMapper setMealMapper;

    @Resource
    private SetMealDishesMapper setMealDishesMapper;

    @Resource
    private DishMapper dishMapper;

    @Resource
    private DishFlavorMapper dishFlavorMapper;


    /**
     * 添加套餐
     *
     * @param setMeal
     * @return
     */
    @Override
    public Boolean save(SetMeal setMeal) {


        List<SetMealDishes> setmealDishes = setMeal.getSetmealDishes();
        if (CollUtil.isNotEmpty(setmealDishes)) {
            setMeal.setId(new Snowflake().nextIdStr());
            setMeal.setCreateUser(TokenHolder.getCurrentId());
            setMeal.setUpdateUser(TokenHolder.getCurrentId());
            setMeal.setUpdateTime(LocalDateTime.now().toString());
            setMeal.setCreateTime(LocalDateTime.now().toString());
            setMeal.setIsDeleted("0");
            for (SetMealDishes setmealDish : setmealDishes) {
                setmealDish.setId(new Snowflake().nextIdStr());
                setmealDish.setSetMealId(setMeal.getId());
                setmealDish.setSort("2");
                setmealDish.setCreateUser(TokenHolder.getCurrentId());
                setmealDish.setUpdateUser(TokenHolder.getCurrentId());
                setmealDish.setUpdateTime(LocalDateTime.now().toString());
                setmealDish.setCreateTime(LocalDateTime.now().toString());
                setmealDish.setIsDeleted("0");
            }

            Integer integer = setMealMapper.insert(setMeal);
            int insert = setMealDishesMapper.insert(setmealDishes);
            return insert > setmealDishes.size();

        }
        return false;
    }

    /**
     * 套餐分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<SetMealPageVo> getPage(Integer page, Integer pageSize, String name) {

        Integer skip = (page - 1) * pageSize;

        List<SetMealPageVo> setMealMapperPage = setMealMapper.getPage(skip, pageSize, name);

        Page<SetMealPageVo> mealList = new Page<>();

        mealList.setTotal(setMealMapperPage.size());
        mealList.setCurrent(page);
        mealList.setSize(pageSize);
        mealList.setRecords(setMealMapperPage);

        return mealList;
    }

    /**
     * 通过套餐id查询
     *
     * @param categoryId
     * @return
     */
    @Override
    public SetMeal getById(String categoryId) {

        SetMeal setMeal = setMealMapper.selectById(categoryId);

        List<SetMealDishes> setMealDishes = setMealDishesMapper.selectById(setMeal.getId());

        setMeal.setSetmealDishes(setMealDishes);

        return setMeal;

    }

    /**
     * 修改
     *
     * @param setMeal
     * @return
     */
    @Override
    public Boolean modifyById(SetMeal setMeal) {
        List<SetMealDishes> setmealDishes = setMeal.getSetmealDishes();
        if (CollUtil.isNotEmpty(setmealDishes)) {
            setMeal.setUpdateTime(LocalDateTime.now().toString());
            setMeal.setUpdateUser(TokenHolder.getCurrentId());

            int i = setMealDishesMapper.deleteBySetMealId(setMeal.getId());
            for (SetMealDishes setmealDish : setmealDishes) {
                setmealDish.setId(new Snowflake().nextIdStr());
                setmealDish.setSetMealId(setMeal.getId());
                setmealDish.setSort("2");
                setmealDish.setCreateUser(TokenHolder.getCurrentId());
                setmealDish.setUpdateUser(TokenHolder.getCurrentId());
                setmealDish.setUpdateTime(LocalDateTime.now().toString());
                setmealDish.setCreateTime(LocalDateTime.now().toString());
                setmealDish.setIsDeleted("0");
            }
            int update = setMealMapper.update(setMeal);
            int insert = setMealDishesMapper.insert(setmealDishes);

            return update > 0;
        }
        return false;
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @Override
    public Boolean remove(String[] ids) {

        if (ArrayUtil.isNotEmpty(ids)) {
            List<String> list = setMealMapper.selectStatusById(ids);
          if(list.contains("1")){
              return false;
          }
            if (CollUtil.isNotEmpty(list)) {
                int i = setMealMapper.updateIsDeletedById(ids);
                int i1 = setMealDishesMapper.updateIsDeletedById(ids);
                return true;
            }

        }
        return false;
    }

    /**
     * 上下架
     *
     * @param ids
     * @return
     */
    @Override
    public Boolean changeStatus(String ids, Integer status) {

        if (StrUtil.isNotEmpty(ids)) {
            String[] split = ids.split(",");
            setMealMapper.updateStatusById(split, status);
            return true;
        }
        return false;
    }

    /**
     * 套餐列表
     *
     * @param categoryId
     * @param status
     * @return
     */
    @Override
    public List<SetMeal> getList(String categoryId, Integer status) {

        List<SetMeal> list = setMealMapper.getList(categoryId, status);

        return list;
    }

    /**
     * 菜品详情
     *
     * @param setMealId
     * @return
     */
    @Override
    public List<DishDetailVo> getDishBySetMealId(String setMealId) {

        List<DishDetailVo> detailList = dishMapper.getDetailBySetMealId(setMealId);

        for (DishDetailVo dishDetailVo : detailList) {
            List<DishFlavor> dishFlavors = dishFlavorMapper.selectById(dishDetailVo.getId());
            dishDetailVo.setFlavors(dishFlavors);
        }
        return detailList;
    }


}
